package com.example.muldatasource.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @ProjectName: muldatasource
 * @Author: qinjiwei
 * @Description:
 * @Date: 2020/6/28 16:36
 * @Version: 1.0
 */
@Configuration
@MapperScan(basePackages = "com.example.muldatasource.dao.second",sqlSessionTemplateRef ="secondSqlSessionTemplate")
public class SecondDataSourceConfig {
    @Autowired
    private SecondDataBaseProperties prop;
    /**本数据源扫描的mapper路径*/
    static final String MAPPER_LOCATION = "classpath:mybatis/mapper/second/*.xml";

    //    创建数据源
    @Bean(name = "secondDS")
    public DataSource getSecondDataSource() {
//        DataSource build =  DataSourceBuilder.create()
//                .driverClassName(prop.driverClassName)
//                .url(prop.url)
//                .username(prop.username)
//                .password(prop.password)
//                .build();
//        return build;

        AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
        ds.setUniqueResourceName("secondDB");

        Properties properties = new Properties();
        properties.put("url",prop.url);
        properties.put("username", prop.username);
        properties.put("password", prop.password);
        ds.setPoolSize(5);
        ds.setXaProperties(properties);
        return ds;
    }


    // 创建SessionFactory
    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDS") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
        try{
            bean.setMapperLocations(resolver.getResources("classpath*:/mapper/second/*.xml"));
//            bean.setConfigLocation(resolver.getResource("classpath:/mybatis-config.xml"));
            return bean.getObject();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResource("classpath*:/mapper/second/*.xml"));
//        return bean.getObject();
    }

    // 创建事务管理器

//    @Bean("secondTransactionManger")
//    public DataSourceTransactionManager secondTransactionManger(@Qualifier("secondDS") DataSource dataSource){
//        return new DataSourceTransactionManager(dataSource);
//    }

    // 创建SqlSessionTemplate

    @Bean(name = "secondSqlSessionTemplate")
    public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    private Class getType(String type) {
        try {
            return Class.forName(type);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
